package hcd.mynote;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ListviewAdapter extends BaseAdapter {

    private List<Note> data;

    private HomeActivityFragment fragment;
    private static LayoutInflater inflater;

    public ListviewAdapter(HomeActivityFragment fragment, List<Note> data){
        this.fragment = fragment;
        this.data = data;

        inflater = (LayoutInflater) fragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
//        if (position == 0) {
//            if (convertView == null){
//                view = inflater.inflate(R.layout.search_bar, null);
//            }
//            final EditText searchText = (EditText) view.findViewById(R.id.search_text);
//            searchText.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//                    fragment.searchNote(s.toString());
//                }
//
//                @Override
//                public void afterTextChanged(Editable s) {
//
//                }
//            });
//
//            Button clearBtn = (Button) view.findViewById(R.id.search_text_clear);
//            clearBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    searchText.setText("");
//                }
//            });
//        }else {
//        }

        if (convertView == null){
            view = inflater.inflate(R.layout.listview_item, null);
        }

        TextView titleView = (TextView) view.findViewById(R.id.list_item_title);
        TextView preview = (TextView) view.findViewById(R.id.list_item_preview);
        TextView timeText = (TextView) view.findViewById(R.id.list_item_time);

        String title = data.get(position).getTitle();
        if (title == null || title.isEmpty()){
            titleView.setText(R.string.no_title);
        }else {
            titleView.setText(title);
        }

        String content = data.get(position).getContent();
        if (content == null || content.isEmpty()){
            preview.setText(R.string.no_content);
        }else {
            preview.setText(content);
        }

        timeText.setText(showTime(data.get(position).getTime()));

        return view;
    }

    private String showTime(long time){
        Date modify = new Date(time);
        if(DateUtils.isToday(time)){
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
            return "今天 " + sdf.format(modify);
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy", Locale.getDefault());
            String year = sdf.format(modify);
            String now = sdf.format(new Date());
            String formatter = "yyyy-MM-dd HH:mm";
            if (year.equals(now)){
                formatter = "MM-dd HH:mm";
            }
            return (new SimpleDateFormat(formatter, Locale.getDefault()).format(new Date(time)));
        }
    }
}
